package top.so.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.UserDTO;
import top.so.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
@SessionAttributes("currUser")//当前用户
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "login",method = {RequestMethod.POST})
    public ModelAndView login(UserDTO userDTO, ModelMap modelMap){
        ModelAndView mv = new ModelAndView();
      List<UserDTO> userDTOList =  userService.selectUser(userDTO);
      //判断结果集中是否有匹配的数据 如有则返回到主页
          for(UserDTO user:userDTOList){
//              mv.addObject("userDTO",user);
              mv.setViewName("redirect:/home");
              modelMap.addAttribute("currUser",user);
              return mv;
          }
      //否则 提示账号或密码错误
      mv.addObject("wrongInfo","账号或密码错误!");
      mv.setViewName("main/login");
        return mv;
    }

    @RequestMapping(value = "goRegister",method = {RequestMethod.GET})
    public ModelAndView goRegister(){
        ModelAndView mv = new ModelAndView("main/register");
        return mv;
    }

    @RequestMapping(value = "doRegister",method = {RequestMethod.POST})
    public ModelAndView doRegister(UserDTO userDTO){
        ModelAndView mv = new ModelAndView();
        List<UserDTO> userDTOList = userService.selectUser(userDTO);
        boolean flag = true;
        for(UserDTO u:userDTOList){
            if(u.getLoginName().equals(userDTO.getLoginName())){
                flag = false;
            }
        }
        if(flag && userService.insertUser(userDTO)){
            mv.addObject("hasCount","yep");
            mv.setViewName("main/login");
        }
        else{
            mv.addObject("hint","注册失败！");
            mv.addObject("wrongInfo","注册账号重复！");
            mv.setViewName("main/fail");
        }
        return mv;
    }


    /*      Spring mvc在调用处理方法前，在请求线程中自动一个隐含的模型对象，调用所有在方法级别标注了@ModelAttribute的方法，并将方法返回值添加到隐含模型中，
        查看Session中是否存在@SessionAttributes(“xxx”)所指定的xxx属性，如果有，将其添加到隐含模型中，
        如果隐含模型已经存在xxx属性，该步骤会覆盖隐含模型中已有的属性值。
        对应标注了@ModelAttribute(“xxx”)方法的入参按如下流程
        如果隐含模型包含了xxx属性，将其赋给该入参，再用请求消息填充该入参对象直接返回，否则如果xxx是会话属性，
        即处理类定义出标注了@SessionAttributes(“xxx”)，则尝试从该会话中获取该属性，并将其值赋给入参，然后再用请求填充该入参对象，
        如会话中找不到xxx属性，则抛出HttpSessionRequiredException，如果隐含模型不存在xxx属性，且xxx也不是会话属性，则创建入参的对象实例，在用请求填充该入参。*/
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ModelAndView logout(HttpSession httpSession,SessionStatus sessionStatus){
        ModelAndView mv = new ModelAndView("redirect:/home");
        httpSession.removeAttribute("currUser");
       // @SessionAttributes需要清除时，使用SessionStatus.setComplete();来清除。注意，它只清除@SessionAttributes的session，不会清除HttpSession的数据
        sessionStatus.setComplete();
        return mv;
    }

}
